/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.template;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.interknit.knitter.cg.layout.LayoutElement;
import net.interknit.knitter.cg.layout.TypeElement;
import net.interknit.knitter.cg.util.Utils;
import net.interknit.knitter.cg.view.SeedTypeHolder;

/**
 *
 */
public class ParameterBeanTemplate extends Template
{
	private String parameterBeanName;
	private final List<String> parameterTypes = new ArrayList<String>();

	public ParameterBeanTemplate addParameterType(String parameterType)
	{
		this.parameterTypes.add(parameterType);
		return this;
	}

	public ParameterBeanTemplate setParameterBeanName(String parameterBeanName)
	{
		this.parameterBeanName = parameterBeanName;
		return this;
	}

	public String getParameterBeanName()
	{
		return parameterBeanName;
	}

	public Iterator<String> getParameterTypes()
	{
		return this.parameterTypes.iterator();
	}

	@Override
	public String getJavaClassName()
	{
		return parameterBeanName;
	}

	@Override
	public String getJavaPackageName()
	{
		return SeedTypeHolder.getInstance().getSeedTypeView().getJavaPackageName() + "." + "args";
	}

	@Override
	public String[] getPlaceholders()
	{
		return new String[] {
				"ADAPTER_NAME",
				"ADAPTER_CLASS_NAME",
				"FIELDS",
				"METHODS",
				"FIELDS_XML"
		};
	}

	@Override
	public LayoutElement getLayoutElement()
	{
		return new TypeElement(this);
	}

	public String getParameterDeclaration()
	{
		return getJavaPackageName() + "." + parameterBeanName + " " + "arg";
	}

	public String getAssignmentStatements()
	{
		StringBuilder sb = new StringBuilder();

		int i = 1;
		for(String parameterType : parameterTypes)
		{
			// output similar to the following:
			// java_util_Set_java_lang_Long_ arg1 = arg.getArg1();
			sb.append("\t" + "\t" + "\t").append(
					parameterType + " arg" + i + " = arg.getArg" + i + "();\n");

			i++;
		}

		return sb.toString();
	}

	public String ADAPTER_NAME()
	{
		return getJavaPackageName() + "." + getParameterBeanName();
	}

	public String ADAPTER_CLASS_NAME()
	{
		return getParameterBeanName();
	}

	public String FIELDS()
	{
		StringBuilder sb = new StringBuilder();

		int i = 1;
		for(String parameterType : parameterTypes)
		{
			sb.append("\t" + "private " + parameterType + " arg" + i + ";" + "\n");
			i++;
		}

		return sb.toString();
	}

	public String METHODS()
	{
		StringBuilder sb = new StringBuilder();

		int i = 1;
		for(String parameterType : parameterTypes)
		{
			sb.append("\t" + "/** getter for " + parameterType + "*/" + "\n");
			sb.append("\t" + "public " + parameterType + " " +
					Utils.deriveGetterName("arg" + i) + "()" + " { return this.arg" + i + "; }" + "\n");
			sb.append("\n");
			sb.append("\t" + "/** setter for " + parameterType + "*/" + "\n");
			sb.append("\t" + "public " + "void" + " " +
					Utils.deriveSetterName("arg" + i) + "(" + parameterType + " arg" + i + ")" +
					" { this.arg" + i + " = arg" + i + "; }" + "\n");
			sb.append("\n");

			i++;
		}

		return sb.toString();
	}

	public String FIELDS_XML()
	{
		StringBuilder sb = new StringBuilder();

		int i = 1;
		for(String parameterType : parameterTypes)
		{
			sb.append("<field name=\"" + "arg" + i + "\" type=\"" + parameterType + "\" />");
			i++;
		}

		return sb.toString();
	}
}
